<template>
  <div style="padding: 10px; box-sizing: border-box">
    <div>
      <a-row :gutter="10">
        <a-col :span="6">
          <div class="box-bg">
            <div class="box-bg-title">
              <span class="title-text">供应商类型分布</span>
            </div>
            <div class="item" ref="homePieChart"></div>
          </div>
        </a-col>
        <a-col :span="12">
          <div class="box-bg">
            <div class="box-bg-title">
              <span class="title-text">历史价格对比</span>
              <div class="title-chose">
                <span
                  v-for="item in data.priceArray"
                  :key="item.key"
                  :class="item.key == data.cur ? 'cur' : ''"
                  @click="changePrice(item)"
                >
                  {{ item.name }}
                </span>
              </div>
            </div>
            <div class="item" ref="homeBarlineChart"></div>
          </div>
        </a-col>
        <a-col :span="6">
          <div class="box-bg">
            <div class="box-bg-title">
              <span class="title-text">潜在供应商风险评估统计</span>
            </div>
            <div class="item" ref="homePieChart3"></div>
          </div>
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, nextTick, reactive, ref, unref, markRaw } from 'vue';
  import { getRiskInfo, getTypeInfo, getHistoryInfo } from '/@/api/erp/supplier/workbench';

  import * as echarts from 'echarts';

  const formate = (name) => {
    let str = name + '企业：';
    data.dataOne.forEach((o: any) => {
      if (name == o.name) {
        str += o.value + '家';
      }
    });
    return str;
  };
  const formateTwo = (name) => {
    let str = name + '：';
    data.dataTwo.forEach((o: any) => {
      if (name == o.name) {
        str += o.value + '家';
      }
    });
    return str;
  };
  const data = reactive({
    dataOne: [],
    // 玫瑰饼图
    pieOption: {
      color: ['#9E9BFC', '#6D68F8', '#7EAAFF'],
      legend: {
        orient: 'horizontal',
        x: 'center',
        y: 'bottom',
        top: '78%',
        itemWidth: 10,
        itemHeight: 10,
        itemGap: 20,
        padding: [5, 5, 5, 5],
        textStyle: {
          color: '#85878e',
        },
        formatter: formate,
      },
      series: {
        name: '',
        center: ['50%', '32%'],
        type: 'pie',
        radius: [0, 85],
        roseType: 'area',
        itemStyle: {
          borderRadius: 0,
        },
        label: {
          color: '#6e7079',
          overflow: 'none',
          formatter: (params) => {
            let per = params.percent || 0;
            return params.name + '\n' + per + '%';
          },
        },
        data: [],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    },

    // 柱状折线图
    priceArray: [] as any[],
    cur: 0,
    myChart3: null as any,
    barOption: {
      color: ['#5E95FF'],
      grid: {
        left: 50,
        right: 40,
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          crossStyle: {
            color: '#999',
          },
        },
      },
      xAxis: [
        {
          type: 'category',
          data: [],
          axisPointer: {
            type: 'shadow',
          },
        },
      ],
      yAxis: [
        {
          type: 'value',
          splitLine: {
            show: true,
            lineStyle: {
              color: ['rgba(204,204,204,0.2)'],
              type: 'solid',
            },
          },
          name: '单位：元',
          min: 0,
          max: 400,
          //interval: 100,
          axisLabel: {
            formatter: '{value}',
          },
        },
      ],
      series: [
        {
          name: '',
          type: 'bar',
          barWidth: 20,
          tooltip: {
            valueFormatter: function (value) {
              return value + ' 元';
            },
          },
          data: [],
        },
      ],
    },
    // 饼图
    dataTwo: [],
    pie3Option: {
      color: ['#02D6DC', '#8C7DFC', '#5E95FF', '#8CB1F9'],
      legend: {
        orient: 'horizontal',
        x: 'center',
        y: 'bottom',
        top: '80%',
        itemWidth: 10,
        itemHeight: 10,
        itemGap: 20,
        padding: [5, 5, 5, 5],
        textStyle: {
          color: '#85878e',
        },
        formatter: formateTwo,
      },
      series: {
        name: '',
        type: 'pie',
        radius: '50%',
        selectedMode: 'single',
        center: ['50%', '38%'],
        label: {
          color: '#6e7079',
          overflow: 'none',
          formatter: (params) => {
            let per = params.percent || 0;
            return params.name + '\n' + per + '%';
          },
        },
        data: [],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    },
  });

  const homePieChart = ref<HTMLDivElement>();
  const homePieChart3 = ref<HTMLDivElement>();
  const homeBarlineChart = ref<HTMLDivElement>();

  onMounted(async () => {
    let res = await getTypeInfo();
    data.dataOne = res || [];
    let res1 = await getHistoryInfo();
    data.priceArray = res1 || [];
    let res2 = await getRiskInfo();
    data.dataTwo = res2 || [];
    data.dataTwo.forEach((o: any) => {
      if (o.name == '及格') {
        o.selected = true;
      }
    });
    data.pieOption.series.data = data.dataOne;
    data.pie3Option.series.data = data.dataTwo;
    nextTick(() => {
      let myChart = markRaw(echarts.init(unref(homePieChart) as HTMLDivElement));
      myChart.setOption(data.pieOption, true);
      myChart.resize(); //显示区域大小发生改变更新图表

      let myChart2 = markRaw(echarts.init(unref(homePieChart3) as HTMLDivElement));
      myChart2.setOption(data.pie3Option, true);
      myChart2.resize(); //显示区域大小发生改变更新图表

      data.myChart3 = markRaw(echarts.init(unref(homeBarlineChart) as HTMLDivElement));
      changePrice(data.priceArray[0]);
    });
  });

  const changePrice = (o) => {
    data.cur = o.key;
    data.barOption.xAxis[0].data = o.category;
    data.barOption.series[0].data = o.data;
    if (o.key === 0) {
      data.barOption.series[0].barWidth = 'auto' as any;
    } else {
      data.barOption.series[0].barWidth = 20;
    }
    data.myChart3.setOption(data.barOption, true);
    data.myChart3.resize(); //显示区域大小发生改变更新图表
  };
</script>

<style lang="less" scoped>
  .title-chose {
    float: right;
    margin-right: 20px;

    span {
      text-align: center;
      padding: 0 12px;
      display: inline-block;
      color: #333;
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      cursor: pointer;
      margin-left: 10px;

      &.cur,
      &:active {
        background-color: #5e95ff;
        color: #fff;
      }
    }
  }

  .box-bg {
    background-color: #fff;
    box-sizing: border-box;
    border-radius: 5px;
    box-shadow: 0 3px 6px 1px rgb(0 0 0 / 10%);

    .box-bg-title {
      border-bottom: 1px solid #eee;
      height: 50px;
      line-height: 50px;
      font-size: 16px;
      box-sizing: border-box;
    }

    .title-text {
      padding: 0 15px;
    }

    .item {
      padding: 10px 10px 0;
      width: 100%;
      height: 340px;
    }
  }

  .ant-col {
    padding: 0 10px;
    margin-bottom: 20px;
  }
</style>
